package com.camel.iamfx.base;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class BaseTableController<E> {

    /**
     * 获取子类表格
     *
     * @return
     */
    protected abstract TableView<E> getTableControl();

    /**
     * 设置操作列
     */
    protected abstract void optColumn();

    public void addTableDataAdapter(Map<String, String> dataMap) {
        new ArrayList<String>();
        ObservableList<TableColumn<E, ?>> columns = getTableControl().getColumns();
        for (TableColumn<E, ?> column : columns) {
            String value = getMapKeyFromValue(dataMap, column.getText());
            if(ObjectUtils.isEmpty(value)) {
                continue;
            }
            column.setCellValueFactory(new PropertyValueFactory<>(value));
        }
    }

    public String getMapKeyFromValue(Map<String, String> map, String v) {
        Set<String> strings = map.keySet();
        for (String key : strings) {
            if(StringUtils.isEmpty(map.get(key))) {
                continue;
            } else {
                if(map.get(key).equals(v)) {
                    return key;
                }
            }
        }
        return null;

    }

}
